﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using GestorSQL.DAO.FileManager;
using GestorSQL.BLL.ImplementationsBridge;
using System.IO;
using System.IO.Compression;

namespace GestorSQL.BLL
{
    public class BLLDescompressReader : IDisposable
    {
        private IFileManager fileManager = ImplementationBridge.GetFileManager("Compress");
        private GZipStream gzIn;
        private StreamReader sr;

        public BLLDescompressReader()
        {

        }

        public void DescompressFile(string path, string fileNameWithOutExtesion, string compressFileName)
        {
            FileInfo file = new FileInfo(Path.Combine(path, string.Format("{0}.{1}", fileNameWithOutExtesion, "txt.gz")));

            if (file.Exists)
            {
                gzIn = new GZipStream(File.OpenRead(file.DirectoryName + "\\" + file.Name),
                CompressionMode.Decompress);

                sr = new StreamReader(gzIn);

            }
        }

        public string ReadLine()
        {
            string input;
            if (sr != null && ((input = sr.ReadLine()) != null))
                return input;
            else
                return null;
        }

        public void Close()
        {

            if (gzIn != null)
                gzIn.Close();

            if (sr != null)
                sr.Close();
        }

        public void Dispose()
        {
            Close();
        }

    }

}
